package com.jy.adscm.spss.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.spss.bean.Base_commodityinfo;
import com.jy.adscm.spss.bean.Base_incomeexpenditure;
import com.jy.adscm.spss.mapper.mysql.Base_incomeexpenditureMapper;
import com.jy.adscm.spss.service.Base_incomeexpenditureService;
import com.jy.adscm.spss.utils.DateUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.NotWritablePropertyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyy
 * @since 2018-11-08
 */
@Service
public class Base_incomeexpenditureServiceImpl extends ServiceImpl<Base_incomeexpenditureMapper, Base_incomeexpenditure> implements Base_incomeexpenditureService {

    @Autowired
    Base_incomeexpenditureMapper baseIncomeexpenditureMapper;

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 加载收入支出的计划金额和实际金额
     * @return
     * @throws ParseException
     */
    @Override
    public List<Base_incomeexpenditure> loadPlanTotal(String szlx) throws ParseException {
//        UpdateWeek();
        List<Base_incomeexpenditure> twoYearWeek = getTwoYearWeek();

        List<Base_incomeexpenditure> planTotal = baseIncomeexpenditureMapper.getPlanTotal(szlx);
        List<Base_incomeexpenditure> practicalTotal = baseIncomeexpenditureMapper.getPracticalTotal(szlx);
        for (Base_incomeexpenditure str : twoYearWeek) {
            for (Base_incomeexpenditure incomeexpenditure : planTotal) {
                if (AdscmBaseUtils.getInstance().isNotEmpty(incomeexpenditure.getWhereWeek(),null)) {
                    if (incomeexpenditure.getWhereWeek().equals(str.getWhereWeek())) {
                        if (incomeexpenditure.getJHJE() != null) {
                            str.setJHJE(incomeexpenditure.getJHJE());
                        }

                    }
                }
            }
            for (Base_incomeexpenditure incomeexpenditure : practicalTotal) {
                if (AdscmBaseUtils.getInstance().isNotEmpty(str.getWhereWeek(),null)){
                    if (incomeexpenditure.getSJWHEREWEEK()!=null){
                        if(incomeexpenditure.getSJWHEREWEEK().equals(str.getSJWHEREWEEK())){
                            str.setSJJE(incomeexpenditure.getSJJE());
                        }
                    }

                }

            }


        }
        return twoYearWeek;
    }

    @Override
    public List<Base_incomeexpenditure> loadBySZInformation(String szlx, String whereWeek) {
        List<Base_incomeexpenditure> bySZInformation=null;
        if("1".equals(szlx)||"2".equals(szlx)){
            bySZInformation=baseIncomeexpenditureMapper.getBySZInformation(szlx, whereWeek);
        }else {
            if ("3".equals(szlx)){
                bySZInformation=baseIncomeexpenditureMapper.getBySZSJInformation("1",whereWeek);
            }else {
                bySZInformation=baseIncomeexpenditureMapper.getBySZSJInformation("2",whereWeek);
            }
        }
        return bySZInformation;
    }

    /**
     * 根据收支计划日期和实际日期修改实际周和计划周
     * @throws ParseException
     */
    public   void   UpdateWeek()  {


        List<Base_incomeexpenditure> date = baseIncomeexpenditureMapper.getDate();
        try {
            for (Base_incomeexpenditure incomeexpenditure : date) {
                if (incomeexpenditure.getJHYF()!=null){
                    baseIncomeexpenditureMapper.updatePlanWeek( DateUtils.getWeek(incomeexpenditure.getJHYF()),incomeexpenditure.getIEID());
                }
                if (incomeexpenditure.getSJSJ()!=null){
                    baseIncomeexpenditureMapper.updatePracticalWeek(DateUtils.getWeek(incomeexpenditure.getSJSJ()),incomeexpenditure.getIEID());
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     *获取收支两年内所有周
     * @return
     */
    private static List<Base_incomeexpenditure> getTwoYearWeek(){
        SimpleDateFormat sd=new SimpleDateFormat("yyyy");
        List<Base_incomeexpenditure> list=new ArrayList<Base_incomeexpenditure>();
        String thisYear = sd.format(new Date());

        for (int j=2018;j<=Integer.valueOf(thisYear)+1;j++){
            int x=30;
            if (j==2018){
                for(int i =x; i<=52;i++){
                    Base_incomeexpenditure base=new Base_incomeexpenditure();
                    if (i<10){
                        base.setWhereWeek(j+":0"+i);
                        base.setSJWHEREWEEK(j+":0"+i);
                    }else {
                        base.setWhereWeek(j+":"+i);
                        base.setSJWHEREWEEK(j+":"+i);

                    }
                    list.add(base);
                }
            }else {
                for(int i =1; i<=52;i++){
                    Base_incomeexpenditure base=new Base_incomeexpenditure();
                    if (i<10){
                        base.setWhereWeek(j+":0"+i);
                        base.setSJWHEREWEEK(j+":0"+i);
                    }else {
                        base.setWhereWeek(j+":"+i);
                        base.setSJWHEREWEEK(j+":"+i);

                    }
                    list.add(base);
                }
            }

        }
        return list;
    }
}
